package com.ax.base.common.controller;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ax.base.common.domain.BaseSubject;
import com.ax.base.common.domain.SysDict;
import com.ax.base.common.domain.Tree;
import com.ax.base.common.service.DictionarySubjectService;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.system.domain.MenuDO;

@Controller
@RequestMapping("/sys/subject")
public class DictionarySubjectController {

	@Autowired
	DictionarySubjectService dictionarySubjectService;
	
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String subject(Model model) {
		return "common/subject/subject";
	}
	
	/**
	 * 字典菜单页面的跳转
	 */
	@GetMapping("/dictionary")
	String dictionary(Model model) {
		return "common/dictionary/dictionary";
	}
	
	/**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return List<BaseSubject>
	 */
	@RequestMapping("/queryBaseSubject")
	@ResponseBody
	public List<BaseSubject> queryBaseSubject(AjaxRes ajaxRes,@RequestParam Map<String, Object> params) {
		List<BaseSubject> baseSubjects = this.dictionarySubjectService.queryBaseSubjectSer(params);
		return baseSubjects;
	  }
	
	/**
	 * 医学科目(模块:医学科目)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseSubject baseSubject
	 */
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName",dictionarySubjectService.getBaseSubject(pId).getName());
		}
		return "common/subject/add";
	}
	
	/**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param BaseSubject baseSubject
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertBaseSubject")
	@ResponseBody
	public AjaxRes insertBaseSubject(AjaxRes ajaxRes,BaseSubject baseSubject){
		try {
			this.dictionarySubjectService.insertBaseSubjectSer(baseSubject);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 医学科目(模块:医学科目)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return BaseSubject baseSubject
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("subject",dictionarySubjectService.getBaseSubject(id));
		return "common/subject/edit";
	}
	
	/**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param BaseSubject baseSubject
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateBaseSubject")
	@ResponseBody
	public AjaxRes updateBaseSubject(AjaxRes ajaxRes,BaseSubject baseSubject){
		try {
			this.dictionarySubjectService.updateBaseSubjectSer(baseSubject);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 医学科目树Tree
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree")
	@ResponseBody
	Tree<MenuDO> insertTree() {
		Tree<MenuDO> tree = dictionarySubjectService.getInsertTree();
		return tree;
	}
	
	/**
	 * 医生端树形选择Tree
	 * 
	 * @param String subjectCode
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/subjectTree")
	@ResponseBody
	Tree<MenuDO> getSubjectOtherTree(String subjectCode) {
		Tree<MenuDO> tree = dictionarySubjectService.getSubjectOtherTree(subjectCode);
		return tree;
	}
	
	/**
	 * Tree
	 * 
	 * @param Long parentOrgId
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree/{parentSubjectId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("parentSubjectId") Long parentSubjectId) {
		Tree<MenuDO> tree = dictionarySubjectService.getSubjectTree(parentSubjectId);
		return tree;
	}
	
	/**
	 * 名字是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@PostMapping("/validate")
	@ResponseBody
	public boolean validate(String parameter){
		return !dictionarySubjectService.validateSer(parameter);
	}
	
	/**
	 * 名字是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@PostMapping("/updateValidate")
	@ResponseBody
	public boolean updateValidate(String parameter){
		return !dictionarySubjectService.updateValidateSer(parameter);
	}
	
	/**
	 * 字典查询(模块:字典)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return PageUtils
	 */
	@RequestMapping("/querySysDict")
	@ResponseBody
	public List<SysDict> querySysDict(AjaxRes ajaxRes,@RequestParam Map<String, Object> params) {
		//	PageUtils sysDicts = this.dictionarySubjectService.querySysDictSer(params);
		    List<SysDict> SysDicts = this.dictionarySubjectService.querySysDictSer(params);
		    return SysDicts;
	  }
	
	/**
	 * 字典详情跳转(新增)
	 * 
	 * @param  无
	 *           
	 * @return HTML
	 */
	@GetMapping("/dictionary/add")
	String add(Model model) {
		return "common/dictionary/add";
	}
	
	/**
	 * 字典详情跳转(修改)
	 * 
	 * @param Long id
	 *           
	 * @return SysDict sysDict
	 */
	@GetMapping("/dictionary/edit/{id}")
	String sysDictEdit(Model model,@PathVariable("id") Long id) {
		model.addAttribute("dictionary",dictionarySubjectService.getQuery(id));
		return "common/dictionary/edit";
	}
	
	/**
	 * 字典新增(模块:字典管理)
	 * 
	 * @param SysDict sysDict
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertSysDict")
	@ResponseBody
	public AjaxRes insertSysDict(AjaxRes ajaxRes,SysDict sysDict){
		try {
			this.dictionarySubjectService.insertSysDictSer(sysDict);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 字典修改(模块:字典管理)
	 * 
	 * @param SysDict sysDict
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateSysDict")
	@ResponseBody
	public AjaxRes updateSysDict(AjaxRes ajaxRes,SysDict sysDict){
		try {
			this.dictionarySubjectService.updateSysDictSer(sysDict);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
